package edu.jmu.bean.user;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import edu.jmu.bean.privilege.Role;
import edu.jmu.bean.system.LoginLog;
/**
 * 用户实体类
 * @author Neal
 *
 */
@Entity
@Table(name="T_USER")
public class User implements Serializable{	
	private static final long serialVersionUID = -7343455656737255332L;
	/**	用户名	**/
	private String userName;
	/**	密码	**/
	private String passWord;	
	/**	性别	**/
	private Gender gender=Gender.MAN;	
	/**	状态	**/
	private State state=State.work;	
	/**	部门	**/
	private Depart depart;
	/**电话号码**/
	private String mobilePhone;
	/**备注**/
	private String remark;
	/**入职时间**/
	private Date registerDate;
	
	/** 管理的部门	**/
	private Set<Depart> departs = new HashSet<Depart>();
	/** 用户组 **/
	private Set<Role> roles = new HashSet<Role>();
	/**	用户日志	**/
	private Set<LoginLog> loginLogs = new HashSet<LoginLog>();
	
	@Id	@Column(length=20)
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	@Column(length=15,nullable=false)
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	@Enumerated(EnumType.STRING)
	@Column(length=5,nullable=false)
	public Gender getGender() {
		return gender;
	}
	public void setGender(Gender gender) {
		this.gender = gender;
	}
	@Enumerated(EnumType.STRING)
	@Column(length=10,nullable=false)
	public State getState() {
		return state;
	}
	public void setState(State state) {
		this.state = state;
	}
	@ManyToOne(cascade=CascadeType.REFRESH,optional=false,fetch=FetchType.EAGER)
	@JoinColumn(name="depart_id")
	public Depart getDepart() {
		return depart;
	}
	public void setDepart(Depart depart) {
		this.depart = depart;
	}
	@OneToMany(mappedBy="principalUser")
	public Set<Depart> getDeparts() {
		return departs;
	}
	public void setDeparts(Set<Depart> departs) {
		this.departs = departs;
	}
	@Column(length=225,nullable=false)
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	@Column(length=11,nullable=false)
	public String getMobilePhone() {
		return mobilePhone;
	}
	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}
	@ManyToMany(cascade=CascadeType.REFRESH, fetch=FetchType.EAGER)
	@JoinTable(name="user_role",inverseJoinColumns=@JoinColumn(name="roleId"),
			joinColumns=@JoinColumn(name="userName"))
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	@OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="user")
	public Set<LoginLog> getLoginLogs() {
		return loginLogs;
	}
	public void setLoginLogs(Set<LoginLog> loginLogs) {
		this.loginLogs = loginLogs;
	}
	@Temporal(TemporalType.TIMESTAMP)
	public Date getRegisterDate() {
		return registerDate;
	}
	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((userName == null) ? 0 : userName.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (userName == null) {
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		return true;
	}
	
	
	
	
}
